﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace FileShredder
{
    class Program
    {
        enum Attribute
        {
            Files,
            OnlyReplace,
            TypeReplace,
            Help
        };

        static int Main(string[] args)
        {
            var dictionaryArgs = GetDictionaryArgs(args);
            if ((bool)dictionaryArgs[Attribute.Help])
                return WriteHelp();

            foreach (string arg in (string[])dictionaryArgs[Attribute.Files])
            {
                var file = new FileInfo(arg.Trim());
                try
                {
                    var shreder = new ShredderFile(file);
                    shreder.ReplaceFile((TypeReplace)dictionaryArgs[Attribute.TypeReplace]);
                    if (!(bool)dictionaryArgs[Attribute.OnlyReplace])
                        shreder.DeleteFile();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(@"File: {0} Error: {1}", file.FullName, ex.Message);
                }
            }
            return 0;
        }
        
        static Dictionary<Attribute, object> GetDictionaryArgs(string[] args)
        {
            var dictionaryArgs = new Dictionary<Attribute, object>
            {
                { Attribute.Help, false },
                { Attribute.OnlyReplace, false },
                { Attribute.TypeReplace, TypeReplace.High },
                { Attribute.Files, null }
            };           
                
            var allParams = string.Join(",", args).Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
            if (allParams.Count() == 0)
                dictionaryArgs[Attribute.Help] = true;

            foreach (var parameters in allParams)
            {
                var param = parameters.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                switch ((Attribute)Enum.Parse(typeof(Attribute), param[0]))
                {
                    case Attribute.Files:
                        {
                            param.Remove(param[0]);
                            dictionaryArgs[Attribute.Files] = param.ToArray();                            
                            break;
                        }
                    case Attribute.OnlyReplace:
                        {
                            dictionaryArgs[Attribute.OnlyReplace] = true;                            
                            break;
                        }
                    case Attribute.TypeReplace:
                        {
                            if (param.Count > 1)
                            {
                                TypeReplace typeReplace;
                                if (Enum.TryParse(param[1], out typeReplace))
                                    dictionaryArgs[Attribute.TypeReplace] = typeReplace;
                                else
                                    throw new NotImplementedException(string.Format("Not Implemented type replace {0}", param[1]));
                            }                        
                            break;
                        }
                    default:
                        {
                            dictionaryArgs[Attribute.Help] = true;
                            break;
                        }
                }                
            }
            return dictionaryArgs;
        }
        
        static int WriteHelp()
        {
            Console.WriteLine("-Files <string> [-OnlyReplace] [-TypeReplace <High|Middle|Simple>]");
            Console.WriteLine(@"Ex1: -Files c:\folder\file1.txt c:\folder\file2.txt");
            Console.WriteLine(@"Ex2: -Files c:\folder\file1.txt -OnlyReplace");
            Console.WriteLine(@"Ex3: -Files c:\folder\file1.txt -TypeReplace Middle");
            return 0;
        }                       
    }    
}
